package cn.iocoder.yudao.module.member.controller.admin.jobintention;

import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.member.controller.admin.jobintention.vo.*;
import cn.iocoder.yudao.module.member.dal.dataobject.jobintention.JobIntentionDO;
import cn.iocoder.yudao.module.member.service.jobintention.JobIntentionService;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

@Tag(name = "管理后台 - 用户求职意向")
@RestController
@RequestMapping("/member/job-intention")
@Validated
public class JobIntentionController {

    @Resource
    private JobIntentionService jobIntentionService;

    @PostMapping("/create")
    @Operation(summary = "创建用户求职意向")
    public CommonResult<Long> createJobIntention(@Valid @RequestBody JobIntentionSaveReqVO createReqVO) {
        return success(jobIntentionService.createJobIntention(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新用户求职意向")
    public CommonResult<Boolean> updateJobIntention(@Valid @RequestBody JobIntentionSaveReqVO updateReqVO) {
        jobIntentionService.updateJobIntention(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除用户求职意向")
    @Parameter(name = "id", description = "编号", required = true)
    public CommonResult<Boolean> deleteJobIntention(@RequestParam("id") Long id) {
        jobIntentionService.deleteJobIntention(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得用户求职意向")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<JobIntentionRespVO> getJobIntention(@RequestParam("id") Long id) {
        JobIntentionDO jobIntention = jobIntentionService.getJobIntention(id);
        return success(BeanUtils.toBean(jobIntention, JobIntentionRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得用户求职意向分页")
    public CommonResult<PageResult<JobIntentionRespVO>> getJobIntentionPage(@Valid JobIntentionPageReqVO pageReqVO) {
        PageResult<JobIntentionDO> pageResult = jobIntentionService.getJobIntentionPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, JobIntentionRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出用户求职意向 Excel")
    @ApiAccessLog(operateType = EXPORT)
    public void exportJobIntentionExcel(@Valid JobIntentionPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<JobIntentionDO> list = jobIntentionService.getJobIntentionPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "用户求职意向.xls", "数据", JobIntentionRespVO.class,
                        BeanUtils.toBean(list, JobIntentionRespVO.class));
    }

}